<?php
namespace helper;
define('HELPER_TIMESTAMP',time());


/*
TODO
添加 ago('now',-30) 三十秒以前对比
*/



class Date {
	public static $format='Y-m-d H:i:s';
	public static $zone='Asia/Shanghai';
	public static $units=[
		'y'=>'年',
		'm'=>'月',
		'd'=>'天',
		'h'=>'小时',
		'i'=>'分钟',
		's'=>'秒'
	];
	public static function create($date=null, $format=null) {
		if(is_object($date) and $date instanceof \DateTime){
			return $date;
		}
		if($date=='now' or is_null($date)){
			$date=time();
		}
		
		// if($format){
		// 	// if(!self::validate($date, $format)){
		// 	// 	throw new \Exception('date "'.$date.'" format "'.$format.'" validate failed.');
		// 	// }
		// 	return (new \DateTime($date))->setTimezone(new \DateTimeZone(self::$zone));
		// }else
		if(is_numeric($date)){
			return (new \DateTime())->setTimezone(new \DateTimeZone(self::$zone))->setTimestamp($date);
		}else{
			return (new \DateTime($date))->setTimezone(new \DateTimeZone(self::$zone));
		}
	}
	public static function validate($date, $format=null) {
		$format = $format?$format:self::$format;
		$d = \DateTime::createFromFormat($format, $date);
		return $d and ($d->format($format) == $date);
	}
	public static function format($date=null,$format=null) {
		$format = $format?$format:self::$format;
		return self::create($date,$format)->format($format);
	}
	public static function data($date=null) {
		$str=self::format($date,'YmdHis');
		$data=[];
		$data['y']=substr($str,0,4);
		$data['m']=substr($str,4,2);
		$data['d']=substr($str,6,2);
		$data['h']=substr($str,8,2);
		$data['i']=substr($str,10,2);
		$data['s']=substr($str,12,2);
		return $data;
	}
	public static function time($date=null) {
		if(is_null($date)) {
			$date=time();
		}
		if(is_numeric($date)) {
			return intval($date);
		}
		return strtotime($date);
	}
	public static function diff($later,$early='now',$format=null) {
		$data=self::diffData($later,$early);
		$result='';
		foreach($data as $key=>$val) {
			if($format and strpos($format,$key)===false) {
				continue;
			}
			$result.=$val . self::$units[$key];
		}
		return $result;
	}
	public static function diffData($later,$early='now') {
		// %	Literal %	%
		// Y	Years, numeric		01, 03
		// y	Years, numeric		1, 3
		// M	Months, numeric		01, 03, 12
		// m	Months, numeric		1, 3, 12
		// D	Days, numeric		01, 03, 31
		// d	Days, numeric		1, 3, 31
		// H	Hours, numeric		01, 03, 23
		// h	Hours, numeric		1, 3, 23
		// I	Minutes, numeric, at least 2 digits with leading 0	01, 03, 59
		// i	Minutes, numeric	1, 3, 59
		// S	Seconds, numeric, at least 2 digits with leading 0	01, 03, 57
		// s	Seconds, numeric	1, 3, 57
		// a	Total number of days as a result of a DateTime::diff() or (unknown) otherwise	4, 18, 8123
		// F	Microseconds, numeric, at least 6 digits with leading 0	007701, 052738, 428291
		// f	Microseconds, numeric	7701, 52738, 428291
		// R	Sign "-" when negative, "+" when positive	-, +
		// r	Sign "-" when negative, empty when positive
		$later=self::create($later);
		$early=self::create($early);
		if($early->getTimestamp() > $later->getTimestamp()) {
			return null;
		}
		$diff=$early->diff($later);
		$result=[];
		foreach(self::$units as $key=>$val) {
			if($_f=$diff->format('%'.$key)) {
				$result[$key]=$_f.$val;
			}
		}
		return $result;
	}
	// 还需要多长时间
	public static function later($later,$early='now') {
		return self::diff($later,$early);
	}
	// 过去了多长时间
	public static function ago($early,$later='now') {
		return self::diff($later,$early);
	}
}